//
//  PayAlertView.swift
//  ViewFoundation
//
//  Created by Kevin on 2020/11/27.
//

import UIKit

protocol PayAlertViewDelegate {
    func pay_alert_view_cancle()
    func pay_alert_view_finish(password:String)
}

class PayAlertView: UIView {
    
    var delegate:PayAlertViewDelegate?
    var callback_cancle:(()->Void)?
    
    // 取消按钮
    private lazy var cancleView:PayCancleView = {
        let cancle = PayCancleView()
        cancle.backgroundColor = UIColor.clear
        cancle.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
        return cancle
    }()
    
    // 弱提示
    private lazy var tipTextView:UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("请输入登录密码", for: .normal)
        btn.setTitleColor(UIColor.lightGray, for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setImage(UIImage(named: "ic_action_lock_closed"), for: .normal)
        btn.imageEdgeInsets = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
        btn.frame = CGRect(x: 0, y: cancleView.frame.maxY, width: bgWidth(), height: 30)
        return btn
    }()
    
    // 输入框
    private lazy var payInputView:PayInputView = {
        let input = PayInputView()
        input.frame = CGRect(x: 15, y: tipTextView.frame.maxY + 15, width: bgWidth() - 30, height: 50)
        return input
    }()
    
    // 分割线
    private lazy var payLineView:UIView = {
        let ui = UIView(frame: CGRect(x: 0, y: payInputView.frame.maxY + 25, width: bgWidth(), height: 1))
        ui.backgroundColor = UIColor(red: 245 / 255.0, green: 245 / 255.0, blue: 245 / 255.0, alpha: 1)
        return ui
    }()
    
    // 文本框
    private lazy var okTextlabelView:UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: payLineView.frame.maxY, width: bgWidth(), height: 50))
        label.text = "确定"
        label.textAlignment = .center
        label.textColor = UIColor.black
        label.isUserInteractionEnabled = true
        label.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    
    // 背景框
    private lazy var bgView:UIView = {
        let bg = UIView()
        let bgW = bgWidth()
        let bgH = okTextlabelView.frame.maxY
        let bgX = (bounds.width - bgW) * 0.5
        let bgY = (bounds.height - bgH) * 0.4
        bg.layer.cornerRadius = 10
        bg.backgroundColor = UIColor.white
        bg.frame = CGRect(x: bgX, y: bgY, width: bgW, height: bgH)
        return bg
    }()
    
    private func bgWidth() -> CGFloat {
        return bounds.width * 0.8
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.init(displayP3Red: 0, green: 0, blue: 0, alpha: 0.4)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        if bgView.contains(cancleView){
            return
        }
        bgView.addSubview(cancleView)
        bgView.addSubview(tipTextView)
        bgView.addSubview(payInputView)
        bgView.addSubview(payLineView)
        bgView.addSubview(okTextlabelView)
        addSubview(bgView)
        
        cancleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(pay_alert_view_cancle)))
        okTextlabelView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(pay_alert_view_finish)))
    }
    
    @objc func pay_alert_view_cancle() -> Void {
        delegate?.pay_alert_view_cancle()
    }
    
    @objc func pay_alert_view_finish() -> Void {
        delegate?.pay_alert_view_finish(password: payInputView.password)
    }
}

class PayCancleView: UIView {
    
    override func draw(_ rect: CGRect) {
        let halfW = rect.width * 0.3
        let halfH = rect.height * 0.3
        
        let path = UIBezierPath()
        // 右斜
        path.move(to: CGPoint(x: halfW, y: halfH))
        path.addLine(to: CGPoint(x: rect.width - halfW, y: rect.height - halfH))
        // 左斜
        path.move(to: CGPoint(x: rect.width - halfW, y: halfH))
        path.addLine(to: CGPoint(x: halfW, y: rect.height - halfH))
        
        path.lineWidth = 2
        UIColor.lightGray.setStroke()
        path.stroke()
    }
}

class PayInputView: UIView,UITextFieldDelegate,PayUITextFieldDelegate {
    
    private var inputs = [UITextField]()
    
    var password:String {
        get {
            var pwd:String = ""
            for item in inputs {
                if let c = item.text {
                    pwd.append(c)
                }
            }
            return pwd
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.clear
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        let margin:CGFloat = 5
        let width = bounds.width / 6
        for item in 0...5 {
            let path = UIBezierPath()
            path.move(to: CGPoint(x: width * CGFloat(item) + margin, y: width))
            path.addLine(to: CGPoint(x: width * CGFloat(item + 1) - margin, y: width))
            UIColor.lightGray.setStroke()
            path.stroke()
        }
    }
    
    override func layoutSubviews() {
        let width = bounds.width / 6
        for item in 0...5 {
            let input = PayUITextField()
            input.tag = item
            input.delegate = self
            input.callback = self
            input.textAlignment = .center
            input.keyboardType = .numberPad
            input.font = UIFont.systemFont(ofSize: 14)
            input.frame = CGRect(x: CGFloat(item) * width, y: 0, width: width, height: width)
            input.textColor = UIColor.black
            if item == 0 {
                input.becomeFirstResponder()
            }
            addSubview(input)
            inputs.append(input)
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        textField.text = string
        if textField.tag < 5 {
            inputs[textField.tag + 1].text = ""
            inputs[textField.tag + 1].becomeFirstResponder()
        }
        if textField.tag == 5 {
            inputs[textField.tag].becomeFirstResponder()
        }
        return false
    }
    
    func deleteBackwardCallback(textField: UITextField) {
        if textField.tag > 0 {
            inputs[textField.tag - 1].text = ""
            inputs[textField.tag - 1].becomeFirstResponder()
        }
    }
}

class PayUITextField: UITextField {
    
    var callback:PayUITextFieldDelegate?
    
    override func deleteBackward() {
        super.deleteBackward()
        callback?.deleteBackwardCallback(textField: self)
    }
}


protocol PayUITextFieldDelegate {
    func deleteBackwardCallback(textField: UITextField)
}
